<?php
declare (strict_types=1);

namespace app\controller\index\upgrade;

// 自动复制升级文件到正式环境
class AutoCopy extends \app\BaseController
{
    /**
     * 升级文件自动复制
     * 作者名：NEMecD1T5JRLEsiV
     * 时间：2025-08-27
     * @param string $type
     */
    public function index($type = 'vip')
    {
        // 个人提交日志
        $svnLog = file_get_contents(public_path() . '/data/svn/index.log');

        // 正则表达式匹配文件路径
        $pattern = '/(已修改|已增加) : ([^\n]+)/';
        preg_match_all($pattern, $svnLog, $matches);
        $filePaths = $matches[2];
        $uniqueFilePaths = array_unique($filePaths);

        $mapDir = [
            'yikatong' => [
                'sourceFolder' => 'D:\project\xiaocao\20250509_test_xsf_yikatong',
                'targetFolder' => 'D:\project\xiaocao\20250515_xsf_yikatong',
            ],
            'vip' => [
                'sourceFolder' => 'D:\project\xiaocao\20250509_test_51huilife_vip',
                'targetFolder' => 'D:\project\xiaocao\20250515_51huilife_vip',
            ],
        ];

        if (!isset($mapDir[$type])) {
            halt('参数错了！');
        }

        // 测试目录
        $sourceFolder = $mapDir[$type]['sourceFolder'];
        // 正式目录
        $targetFolder = $mapDir[$type]['targetFolder'];

        foreach ($uniqueFilePaths as $path) {
            $sourceFile = $sourceFolder . $path;
            $sourceFile = trim($sourceFile);
            if (!file_exists($sourceFile)) {
                continue;
            }

            $targetFile = trim($targetFolder . $path);
            if (!is_dir(dirname($targetFile))) {
                mkdir(dirname($targetFile), 0755, true);
            }
            echo $sourceFile . '<br />';
            if (is_file($sourceFile)) {
                copy($sourceFile, $targetFile);
            }
        }
    }
}
